package com.typy.www.tpay.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.vm.BuyConfigVm;
import com.typy.www.tpay.bean.vm.NoticeVm;
import com.typy.www.tpay.ui.itemview.NoticeItemView;
import com.typy.www.tpay.ui.itemview.RadioItemView;
import com.typy.www.tpay.util.Logutil;

import java.util.List;

/**
 * Created by 13799 on 2018/7/28.
 */

public class RadioAdapter extends BaseAdapter{
private Context mContext;
    private List<BuyConfigVm> buyConfigList;
    private BuyConfigListener listener;

    public BuyConfigListener getListener() {
        return listener;
    }

    public void setListener(BuyConfigListener listener) {
        this.listener = listener;
    }

    public RadioAdapter(Context context){
        mContext=context;
    }

    public List<BuyConfigVm> getBuyConfigList() {
        return buyConfigList;
    }

    public void setBuyConfigList(List<BuyConfigVm> buyConfigList) {
        this.buyConfigList = buyConfigList;
    }

    @Override
    public int getCount() {
        return buyConfigList==null?0:buyConfigList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        RadioItemView itemView=null;
        if(convertView==null){
            itemView= new RadioItemView(mContext);
            convertView=itemView.getmView();
            convertView.setTag(itemView);
        }else {
            itemView= (RadioItemView) convertView.getTag();
        }
        itemView.bind(buyConfigList.get(i),i);
        itemView.setListener(new RadioItemView.Listener() {
            @Override
            public void updata(int position) {
                updatas(position);
            }
        });
//        getVm();
        return convertView;
    }

    private void updatas(int position){
        for (int i=0;i<buyConfigList.size();i++){
            if(position==i){
                buyConfigList.get(i).setCheck(true);
            }else{
                buyConfigList.get(i).setCheck(false);
            }
        }
        getVm();
        notifyDataSetChanged();
    }
    public void getVm(){
        for (int i=0;i<buyConfigList.size();i++){
            if(buyConfigList.get(i).isCheck()){
                listener.getBuyConfig(buyConfigList.get(i));
            }
        }
    }
    public interface BuyConfigListener{
        void getBuyConfig(BuyConfigVm vm);
    }
}
